package client;

import entity.LogRec;
import entity.MatchedLogRec;
import exceptions.GatherException;

import java.util.List;

public class DataGather {
	private DataSender sender;
	private DataReader reader;
	private DataWriter writer;
	private DataMatcher matcher;
	private Configuration config;
	
	public DataGather(Configuration cfg) {
		config=cfg;
		//日志文件路径和未匹配登录日志文件
		reader= new DataReader(config.getLogFileName(),config.getLoginsPath());
		matcher= new DataMatcher(config.getLoginsPath(),config.getLabIP());//保存未匹配登录日志文件
		writer = new DataWriter(config.getTargetPath());//最终生成的匹配文档
		sender= new DataSender(config.getServerIP(),config.getServerPort());
	}

	public void gather() throws GatherException{
		try {
			List<LogRec> list = reader.getAllLogs();
			List<MatchedLogRec> matchedList = matcher.matchLogs(list);
			writer.writeLogs(matchedList);
			sender.send(matchedList);
		} catch (Exception e) {
			e.printStackTrace();
			throw new GatherException(e);
		}
		
	}
}
